<?php

namespace Api\Controller;

// 玩家的视频管理
class MyVideoController extends UserBaseController {

	const MATCH_USER_GROUP = 5; // 固定的赛事视频发布组ID

	// 提交视频信息
	public function submit() {
		$this->checkAuth('video');
		$data = $this->requestFilter(['title', 'game_id', 'tags', 'cover', 'uri', 'size', 'thumb', 'pre_uri', 'time', 'detail', 'dpi', 'kbps', 'platform', 'md5', 'dpi_width', 'dpi_height', 'need_audit']);

		$Event = event('Video');
		if (!$Event->checkMD5($data['md5'])){
			return $this->error(self::$API_ERROR['VIDEO_DUPLICATE'], '信息重复');
		}


		$service = service('Video');
		// $data  = I('request.');
		if ($data['title']) {
			$_REQUEST['full_title'] = $data['full_title'] = $data['title'];
		}

		#TODO 获取用户名
		$data['uid'] = UID;

		// 如果是赛事组则取status为 PEDDING
		// if ($this->isMatchGroup(UID)) {
		// 	$data['status'] = $service::STATUS_PEDDING;
		// } else {
		// 	$data['status'] = C('AUTO_VIDEO_OPEN', null, false) ? $service::STATUS_PASS : $service::STATUS_PEDDING;
		// }

		// 上传视频后默认不开启，等待转码完成后开启 liubo 2016-8-8
		$data['status'] = $service::STATUS_PEDDING;

		$res = $Event->add($data);
		if (!$res) {
			$this->error(500, $Event->getError());
		}

		$this->success('投稿成功', ['vid' => $res]);
	}

	public function uploader_submit()
	{
		$_POST['need_audit'] = $_REQUEST['need_audit'] = 1;
		$this->submit();
	}

	protected function isMatchGroup($uid) {
		// return D('AuthGroupAccess')->where(['uid' => $uid, 'group_id' => self::MATCH_USER_GROUP])->count() > 0;
		$user = service('Users')->find($uid);
		return in_array('match', $user['groups']);
	}

	public function checkVideoInfo() {
		$data = $this->requestFilter(['title', 'detail']);
		if (!event('Sensitive')->check($data['title'])) {
			$this->error(500, '标题非法');
		}
		if (!event('Sensitive')->check($data['detail'])) {
			$this->error(500, '描述非法');
		}
		$this->success('数据合法');
	}

	/**
	 * 删除视频接口
	 * 用户主动操作、逻辑删除
	 * @author liubo <liubo@ilongyuan.com.cn>
	 */
	public function delete() {
		$this->checkAuth('video');
		$vid = I('vid', 0, 'intval');
		if (!UID) {
			$this->error(500, '用户信息有误');
		}
		$vEvent = event('Video');

		$res = $vEvent->deleteVideo($vid, UID);
		if (!$res) {
			$this->error(500, $vEvent->getError());
		}
		$this->success('删除成功');
	}

}